<?php
/**
 * SimplifyPHP Framework
 *
 * This file is part of SimplifyPHP Framework.
 *
 * SimplifyPHP Framework is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * SimplifyPHP Framework is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * @author Rodrigo Rutkoski Rodrigues, <rutkoski@gmail.com>
 * @copyright Copyright 2008 Rodrigo Rutkoski Rodrigues
 */

require_once ('HTMLElement.php');

/**
 *
 * @author Rodrigo Rutkoski Rodrigues <rutkoski@gmail.com>
 *
 */
class JavascriptHelper extends Helper
{

  protected $content = array();

  protected $minifyAll = true;

  /**
   *
   * @param $src
   * @param $inline
   * @return string
   */
  public function link($src, $inline = false)
  {
    if (! sy_url_is_absolute($src)) {
      $src = sy_fix_url($this->config->get('app_url') . '/javascript/' . $src);
    }

    $src = sy_fix_extension($src, 'js');

    $link = new HTMLElement('script');
    $link->attr('src', $src);
    $link->attr('type', "text/javascript");

    return $this->output($link) . "\n";
  }

  /**
   *
   * @param string $file Absolute or relative path to css file, with or without extension
   * @param boolean $minify
   * @param boolean $inline
   * @return void
   */
  public function load($file, $minify = true, $inline = true)
  {
    if (! sy_url_is_absolute($file)) {
      $file = sy_fix_url($this->config->get('app_url') . '/javascript/' . $file);
    }

    $file = sy_fix_extension($file, 'js');

    $this->minifyAll = $this->minifyAll && $minify;
    $this->content[] = array('minify' => $minify, 'inline' => $inline, 'file' => $file, 'content' => file_get_contents($file));
  }

  public function dump($minify = true)
  {
    $output = '';

    $output .= '<script>';

    $js = '';
    foreach ($this->content as $row) {
      if ($minify && $row['minify'] && ! $this->minifyAll) {
        $js .= $this->minify($row['content']);
      }
      else {
        $js .= $row['content'];
      }
    }

    if ($minify && $this->minifyAll) {
      $js = $this->minify($js);
    }

    $output .= $js . '</script>';

    return $this->output($output);
  }

  protected function minify($js)
  {
    if (is_dir(SY_DIR . '/lib/minify')) {
      set_include_path(SY_DIR . '/lib/minify/lib/' . PATH_SEPARATOR . get_include_path());
      require_once ('JSMin.php');
      return JSMin::minify($js);
    }

    return $js;
  }

  public function start()
  {
    ob_start();
  }

  public function end($minify = true, $inline = true)
  {
    $content = ob_get_clean();
    $content = preg_replace('#</?script( .*?)?>#i', '', $content);
    $this->content[] = array('minify' => $minify, 'inline' => $inline, 'content' => $content);
  }

  /**
   *
   */
  public function block($content, $inline = false)
  {
    return $this->output($this->blockOpen() . $content . $this->blockClose()) . "\n";
  }

  /**
   *
   */
  public function blockOpen()
  {
    return $this->output("<script type=\"text/javascript\">") . "\n";
  }

  /**
   *
   */
  public function blockClose()
  {
    return $this->output("</script>") . "\n";
  }

}

?>